package com.greenant.store.view;

import java.util.ArrayList;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenant.store.R;
import com.greenant.store.adapter.EmrecvInportListAdapter;
import com.greenant.store.bean.EMRecv;
import com.greenant.store.bean.EMRecvList;
import com.greenant.store.bean.OrderList;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.iLog;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExpressTabView3 extends RelativeLayout {

	private Context mContext;

	private final String TAG = "ExpressTabView3";
	private EmrecvInportListAdapter emrecvInportListAdapter;

	private List<EMRecv> emrecvList = new ArrayList<EMRecv>();

	private TextView noOrderListHint;
	private ListView emrecvlist;
	private RelativeLayout nodataView;

	private int totalItemCounts;
	private int expectedCount = 50;

	private boolean isGettingData = false;

	public ExpressTabView3(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public ExpressTabView3(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public ExpressTabView3(Context context) {
		super(context);
		mContext = context;
		init();
	}

	private void init() {
		View view = LayoutInflater.from(mContext).inflate(R.layout.view_express_tab3, this);
		emrecvlist = (ListView) view.findViewById(R.id.emrecvlist);
		emrecvInportListAdapter = new EmrecvInportListAdapter(emrecvList, mContext);
		emrecvlist.setAdapter(emrecvInportListAdapter);

		noOrderListHint = (TextView) view.findViewById(R.id.noOrderListHint);
		nodataView = (RelativeLayout) view.findViewById(R.id.nodataView);

		emrecvlist.setOnScrollListener(new AbsListView.OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (totalItemCount < totalItemCounts && (firstVisibleItem + visibleItemCount) == (totalItemCount - 2)) {

					loadMoreData();
				}
			}
		});
		getFirstData();
	}

	public void getFirstData() {

		String userId = "";

		AVUser user = AVUser.getCurrentUser();
		if (user != null) {
			userId = user.getObjectId();
		}
		// 代收，代发
		// TODO 558e0fe4e4b060308e3cb18c userId
		RetrieveDataAsyn.getInstance().getEMRecvExportInport(userId, EMRecv.EMRECV_TYPE_INPORT, 0, expectedCount, 0,
				new RetrieveDataListener() {

					@Override
					public void dataRetrieved(int requestId, Object data) {
						emrecvList.clear();
						refreshWithData();
						iLog.d(TAG, "onRefreshComplete:" + data);
						if (data != null) {

							EMRecvList orderList = (EMRecvList) data;
							totalItemCounts = orderList.getTotalNum();
							emrecvList.addAll(orderList.getEmrecvList());
							if (emrecvList.size() == 0) {
								refreshNoData();
							}
						} else {
							refreshNoData();
						}
						emrecvInportListAdapter.notifyDataSetChanged();

					}
				});

	}

	private void loadMoreData() {
		if (!isGettingData) {
			isGettingData = true;

			String userId = "";

			AVUser user = AVUser.getCurrentUser();
			if (user != null) {
				userId = user.getObjectId();
			}
			// 代收，代发
			RetrieveDataAsyn.getInstance().getEMRecvExportInport(userId, EMRecv.EMRECV_TYPE_INPORT, emrecvList.size(), expectedCount, 0,
					new RetrieveDataListener() {

						@Override
						public void dataRetrieved(int requestId, Object data) {
							if (data != null) {
								EMRecvList orderList = (EMRecvList) data;
								totalItemCounts = orderList.getTotalNum();
								emrecvList.addAll(orderList.getEmrecvList());
								emrecvInportListAdapter.notifyDataSetChanged();
							}
							isGettingData = false;
						}
					});

		}
	}

	public void refreshNoData() {
		noOrderListHint.setVisibility(View.VISIBLE);
		emrecvlist.setVisibility(View.INVISIBLE);
		nodataView.setVisibility(View.INVISIBLE);
	}

	public void refreshWithData() {
		noOrderListHint.setVisibility(View.INVISIBLE);
		emrecvlist.setVisibility(View.VISIBLE);
		nodataView.setVisibility(View.INVISIBLE);
	}

}
